// index.js
Page({
    data: {
        banners: [], //轮播图列表
        isType: 0, //类型
        booksList: ["aa", "bb", "cc"], //图书列表
        pagea: 1, //当前热门页数
        pageb: 1, //当前最新页数
    },
    onLoad: function (options) {
        // 从后台获取轮播图数据
        this.setBanersData();
        this.setBooksListData(0);
    },
    //上拉触底
    onReachBottom() {
        console.log("上拉触底");
        //获取向更多的相同种类的图书信息
        // 1.当前类型
        let typeId = this.data.isType;
        // 2.当前页数
        let pagea = this.data.pagea;
        let pageb = this.data.pageb;
        if (typeId == 0) {
            this.setData({
                pagea: ++this.data.pagea
            })
            this.loadMoreBooksList(typeId, this.data.pagea)
        } else {
            this.setData({
                pageb: ++this.data.pageb
            })
            this.loadMoreBooksList(typeId, pageb)
        }
    },
    loadMoreBooksList(typeId, page) {
        wx.showLoading({
                title: '数据加载中',
            }),
            console.log(typeId, page);
        let that = this;
        wx.request({
            url: `http://192.168.0.82:8080/bookshoop/books/query?state=${typeId}&&page=${page}`,
            data: {},
            header: {
                "content-tye": "application/json"
            },
            success: function (res) {

                if (res.statusCode == 200) {
                    console.log(res.data.list);
                    // 将之前的数据和现在的数据合并
                    setTimeout(() => {
                        wx.hideLoading();
                        that.setData({
                            booksList: res.data.list.concat(that.data.booksList)
                        })
                    }, 2000)
                }
            },

            fail(res) {},
            complete() {}
        })

    },
    // 用于获取轮播图数据
    setBanersData() {
        // this指向，转移
        let that = this;
        console.log("4141212")
        // 1.使用request
        wx.request({
            url: 'http://192.168.0.82:8080/bookshoop/books/query?state=2',
            data: {},
            header: {
                "content-tye": "application/json"
            },
            success: function (res) {
                if (res.statusCode == 200) {
                    console.log(res.data.list);
                    // 将轮播图数据保存起来，保存到banners
                    that.setData({
                        banners: res.data.list
                    })
                    console.log(res.data.list);
                }
            },
            fail(res) {},
            complete() {}
        })
    },
    // 实现选项卡
    toggletype(e) {
        // console.log(e.currentTarget.dataset.typeId);
        //重置类型的显示当前页数
        this.setData({
            pagea: 1,
            pageb: 1,
            isType: e.currentTarget.dataset.typeId
        })
        // 获取以类型下对应的图书数据
        this.setBooksListData(e.currentTarget.dataset.typeId)
    },
    // 获取图书列表bookslist所有数据信息
    setBooksListData(typeId) {
        let that = this;
        wx.request({
            url: `http://192.168.0.82:8080/bookshoop/books/query?state=${typeId}`,
            data: {
                page: "",
            },
            header: {
                "content-tye": "application/json"
            },
            success: function (res) {
                /////////////////////////////
                if (res.statusCode == 200) {
                    console.log(res.data.list);
                    // 将轮播图数据保存起来，保存到banners
                    that.setData({
                        booksList: res.data.list
                    })

                }
            },
            fail(res) {},
            complete() {}
        })
    },
    // 实现搜索更多图书
    searchBar(e) {
        // 拿到想要搜索的图书title
        // console.log(e.detail.value.val);
        // 拿到当前的类型
        // console.log(this.data.isType);
        let that = this;
        wx.request({
            url: `http://192.168.0.82:8080/bookshoop/books/query?state=${this.data.isType}&&title=${e.detail.value.val}&&rows=25`,
            data: {
                page: "",
            },
            header: {
                "content-tye": "application/json"
            },
            success: function (res) {

                if (res.statusCode == 200) {
                    console.log(res.data.list);
                    // 将轮播图数据保存起来，保存到banners
                    that.setData({
                        booksList: res.data.list
                    })
                }
            },
            fail(res) {},
            complete() {}
        })
    },

})